// src/stores/useLoginUserStore.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getLoginUserUsingGet } from '@/api/userController.ts'

// 定义用户类型
interface UserInfo {
  id?: number
  userName: string
  userRole?: string
  [key: string]: any
}

export const useLoginUserStore = defineStore('loginUser', () => {
  const loginUser = ref<UserInfo>({
    userName: '未登录',
    userRole: 'user' // 默认为普通用户
  })

  async function fetchLoginUser() {
    try {
      const res = await getLoginUserUsingGet();
      if (res.data.code === 0 && res.data.data) {
        loginUser.value = res.data.data;
      }
    } catch (error) {
      console.error('获取登录用户信息失败:', error)
    }
  }

  function setLoginUser(newLoginUser: UserInfo) {
    loginUser.value = newLoginUser
  }

  return { loginUser, setLoginUser, fetchLoginUser }
})
